﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.Diagnostics;

namespace Test_Drag_N_drop
{
    class ResultZone
    {
        private Rectangle myBox;
        private static ResultZone instance;
        private Vector2 player;
        private int[,] map;
        private Texture2D playerTexture;
        private Texture2D wallTexture;
        private Texture2D wayTexture;
        private Texture2D backmazeTexture;
        private Vector2 startDrawPoint;
        private Stopwatch myTimer;
        private bool isRunning = false;
        private float nextMoveTime;
        private ConstructBlock currentBlock;
        private Texture2D backTexture;

        private ResultZone()
        {

        }

        public static ResultZone getInstance()
        {
            if (instance == null)
            {
                instance = new ResultZone();
            }
            return instance;
        }

        public void Initialize(int sW, int sH)
        {
            map = new int[3, 5] { { 0, 1, 1, 1, 1 }, { 0, 0, 0, 0, 1 }, { 1, 1, 1, 2, 1 }};
            player = new Vector2(0, 0);
            myTimer = new Stopwatch();
            myBox = new Rectangle(0, 0, (int)sW / 3, sH);
            startDrawPoint = new Vector2((int)sW / 6 - 45, sH / 2 - 75);
        }

        public void LoadContent (ContentManager content)
        {
            backTexture = content.Load<Texture2D>("resultpanel");
            playerTexture = content.Load<Texture2D>("Player");
            wayTexture = content.Load<Texture2D>("Waymaze");
            wallTexture = content.Load<Texture2D>("Wallmaze");
            backmazeTexture = content.Load<Texture2D>("Backmaze");
        }

        public int playerDoAction(TypeOfBlock action)
        {
            switch (action)
            {
                case TypeOfBlock.UP :
                    {
                        if (!(player.Y - 1 < 0) && !(map[(int)player.X,(int)player.Y -1] == 1))
                        {
                            player.Y -= 1;
                        }
                        else
                        {
                            System.Diagnostics.Debug.WriteLine("You hit the wall : Try Again");
                            return 1;
                        }

                        break;
                    }

                case TypeOfBlock.DOWN:
                    {
                        if (!(player.Y + 1 > 4) && !(map[(int)player.X, (int)player.Y + 1] == 1))
                        {
                            player.Y += 1;
                        }
                        else
                        {
                            System.Diagnostics.Debug.WriteLine("You hit the wall : Try Again");
                            return 1;
                        }

                        break;
                    }

                case TypeOfBlock.LEFT:
                    {
                        if (!(player.X - 1 < 0) && !(map[(int)player.X -1 , (int)player.Y] == 1))
                        {
                            player.X -= 1;
                        }
                        else
                        {
                            System.Diagnostics.Debug.WriteLine("You hit the wall : Try Again");
                            return 1;
                        }

                        break;
                    }

                case TypeOfBlock.RIGHT:
                    {
                        Debug.WriteLine((int)(player.X + 1) + ", " + ((int)player.Y));
                        if (!(player.X + 1 > 3) && !(map[(int)player.X +1 , (int)player.Y] == 1))
                        {
                            player.X += 1;
                        }
                        else
                        {
                            System.Diagnostics.Debug.WriteLine("You hit the wall : Try Again");
                            return 1;
                        }

                        break;
                    }


                case TypeOfBlock.END:
                    {
                        if (map[(int)player.X, (int)player.Y] == 2)
                        {
                            System.Diagnostics.Debug.WriteLine("You win !");
                            return 3;
                        }
                        else
                        {
                            System.Diagnostics.Debug.WriteLine("You didn't reach the exit !");
                            return 2;
                        }
                    }
            }
                return 0;

        }

        public void Draw(SpriteBatch spriteBatch, GameTime gameTime)
        {
            spriteBatch.Draw(backTexture, myBox, Color.White);
            spriteBatch.Draw(backmazeTexture, new Vector2(startDrawPoint.X - 5, startDrawPoint.Y - 5), Color.White);
            for (int i = 0; i<3; i++)
            {
                for (int j= 0; j<5; j++)
                {
                    if (map[i, j] == 0 || map[i, j] == 2)
                    {
                        spriteBatch.Draw(wayTexture, new Vector2(startDrawPoint.X + i * 30, startDrawPoint.Y + j *30), Color.White);
                    }
                    else
                    {
                        spriteBatch.Draw(wallTexture, new Vector2(startDrawPoint.X + i * 30, startDrawPoint.Y + j * 30), Color.White);
                    }
                }
            }
            spriteBatch.Draw(playerTexture,new Vector2( startDrawPoint.X + player.X * 30, startDrawPoint.Y + player.Y * 30), Color.White);
        }

        public bool isIn(Rectangle rect)
        {
            if (myBox.Width > rect.X)
            {
                return true;
            }
            else 
            {
                return false;
            }
        }

        public int run ()
        {
            if (!isRunning)
            {
                myTimer.Start();
                nextMoveTime = myTimer.ElapsedMilliseconds + 1000;
                currentBlock = ConstructZone.getInstance().getList()[0];
                currentBlock.isCurrentBlock(true);
                isRunning = true;
            }
            else
            {   
                if (myTimer.ElapsedMilliseconds > nextMoveTime)
                {
                    nextMoveTime = myTimer.ElapsedMilliseconds + 1000;
                    if (currentBlock.HaveSon)
                    {
                        currentBlock.isCurrentBlock(false);
                        currentBlock = currentBlock.Son;
                        currentBlock.isCurrentBlock(true);
                        int result = playerDoAction(currentBlock.BlockType);
                        if (result == 0)
                        {
                            return 0;
                        }
                        else
                        {
                            player = Vector2.Zero;
                            isRunning = false;
                            return result;

                        }
                    }
                    else
                    {
                        player = Vector2.Zero;
                        isRunning = false;
                        return 4;
                    }
                }
            }
            

            return 0;
        }
    }
}
